package rnavigator.model;

import java.util.HashMap;
import java.util.LinkedList;

/**
 *
 * @author Elias la Cour
 */
public class IOHandler {

    /**
     * Creates a long sequence of nucleotides
     * @return Returns a Structure
     */
    private boolean debug = false;

    /**@standardizeInput()
     * method to make sure all lengths, characters etc are valid,
     * and to create a new structure object
     */
    public void standardizeInput() {
    }

    public Structure createInputSeq10000() {
        String[] nucTypes = {"A", "U", "C", "G"};
        String[] seq = new String[10001];
        seq[0] = "s";
        for (int i = 1; i < seq.length; i++) {
            seq[i] = nucTypes[i % 4];
        }
        Structure s = new Structure();
        s.setBaseSequence(seq);
        return s;
    }

    /**
     * a small test input containing sequence as well as structure + pseudoknots
     * @return Returns a Structure
     */
    public Structure smallInput() {
        String inSeq = "UUAGCUCGCCAGUUAGCGAGGUCUGUCCCCACACGACAGAUAAUCGGGUGCAACUCCCGCCCCUUUUCCGAGGGUCAUCGGAACCA";
        String inDotSequence = "::::(((((::::::))))):((((((:::::::))))))::::((((:::::::))))(((:::[[[[[[))):::]]]]]]:::";
        String[] sequence = new String[inSeq.length()];
        String[] dotSequence = new String[inDotSequence.length()];
        for (int i = 0; i < inSeq.length(); i++) {
            sequence[i] = inSeq.substring(i, i + 1);
            dotSequence[i] = inDotSequence.substring(i, i + 1);
        }
        Structure s = new Structure();
        s.setDotSequence(dotSequence);
        s.setBaseSequence(sequence);

        if (debug) {
            System.out.print("length of basesequence is " + s.getBaseSequence().length + "\n");
            for (String seq : s.getBaseSequence()) {
                System.out.print(seq);
            }
            System.out.println();
        }
        return s;
    }

    public Structure smallInputNoPseudo() {
        String inSeq = "UUAGCUCGCCAGUUAGCGAGGUCUGUCCCCACACGACAGAUAAUCGGGUGCAACUCCCGCCCCUUUUCCGAGGGUCAUCGGAACCA";
        String inDotSequence = "::::(((((::::::))))):((((((:::::::))))))::::((((:::::::))))(((:::::::::)))::::::::::::";
        String[] sequence = new String[inSeq.length()];
        String[] dotSequence = new String[inDotSequence.length()];
        for (int i = 0; i < inSeq.length(); i++) {
            sequence[i] = inSeq.substring(i, i + 1);
            dotSequence[i] = inDotSequence.substring(i, i + 1);
        }
        Structure s = new Structure();
        s.setDotSequence(dotSequence);
        s.setBaseSequence(sequence);

        if (debug) {
            System.out.print("length of basesequence is " + s.getBaseSequence().length + "\n");
            for (String seq : s.getBaseSequence()) {
                System.out.print(seq);
            }
            System.out.println();
        }
        return s;
    }

    public Structure verySmallInput() {
        String[] sequence = {"C", "C", "C", "A", "A", "A", "A", "A", "G", "G", "G"};
        String[] dotSequence = {"(", "(", "(", ":", ":", ":", ":", ":", ")", ")", ")"};
        Structure s = new Structure();
        s.setDotSequence(dotSequence);
        s.setBaseSequence(sequence);

        if (debug) {
            System.out.print("length of basesequence is " + s.getBaseSequence().length + "\n");
            for (String seq : s.getBaseSequence()) {
                System.out.print(seq);
            }
            System.out.println();
        }
        return s;
    }

    public Structure testcase10k() {
        String[] sequence = new String[10000];
        //for (String str : sequence) {
        for (int i = 0; i < sequence.length; i++) {
            String str = "A";
            sequence[i] = str;
        }

        String[] dotSequence = new String[10000];
        for (int j = 0; j < dotSequence.length; j++) {
            String d = ":";
            dotSequence[j] = d;
        }
        for (int k = 2 ; k<2502;k++){
            dotSequence[k]="(";
        }
        for (int l = 5002 ; l<7502;l++){
            dotSequence[l]=")";
        }
//        int numberOfNucleotides = sequence.length;
//        for (int j = 0; j < 2500; j++) {
//            int p1 = (int) (Math.random() * numberOfNucleotides);
//            int p2 = (int) (Math.random() * numberOfNucleotides);
//            if (dotSequence[p1].equals(":") && dotSequence[p2].equals(":")) {
//                dotSequence[p1] = "(";
//                dotSequence[p2] = ")";
//            } else {
//                boolean match = false;
//                while (!match) {
//                    p1 = (int) (Math.random() * numberOfNucleotides);
//                    p2 = (int) (Math.random() * numberOfNucleotides);
//                    if (dotSequence[p1].equals(":") && dotSequence[p2].equals(":")) {
//                        dotSequence[p1] = "(";
//                        dotSequence[p2] = ")";
//                        match = true;
//                    }
//                }
//            }
        //}
//        int testStart = 0;
//        int testEnd = 0;
//        for (String sttest : dotSequence) {
//            if (sttest.equals("(")) {
//                testStart++;
//            }
//            if (sttest.equals(")")) {
//                testEnd++;
//            }
//        }
//        System.out.println(testStart+" "+testEnd+" ");
        
        Structure s = new Structure();
        s.setDotSequence(dotSequence);
        s.setBaseSequence(sequence);
        if (debug) {
            System.out.print("length of basesequence is " + s.getBaseSequence().length + "\n");
            for (String seq : s.getBaseSequence()) {
                System.out.print(seq);
            }
            System.out.println();
        }
        return s;
    }

    /**
     * @param debug the debug to set
     */
    public void setDebug(boolean debug) {
        this.debug = debug;
    }
}
